package com.sgb.kjwl.view.ui.activity.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.viewpager.widget.ViewPager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragCorporateWebsiteDispalyBinding;
import com.swgk.core.view.entity.EnterpriseEditEntity;
import com.sgb.kjwl.view.ui.adapter.home.TabMenuAdapter;
import com.sgb.kjwl.viewmodel.home.CorporateWebsiteModel;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;
import com.swgk.core.util.ResolutionUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 企业网站展示界面（新进入网站）
 * Create by LiWei 2021/11/24
 */
public class CorporateWebsiteDisplayFragment extends AppLazyFragment {

    private FragCorporateWebsiteDispalyBinding binding;
    private CorporateWebsiteModel viewModel;

    public static CorporateWebsiteDisplayFragment newInstance(String key) {
        CorporateWebsiteDisplayFragment fragment = new CorporateWebsiteDisplayFragment();
        Bundle bundle = new Bundle();
        bundle.putString("key", key);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.frag_corporate_website_dispaly, container, false);
        viewModel = new ViewModelProvider(this).get(CorporateWebsiteModel.class);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        viewModel.menuList.observe(getActivity(), new Observer<List<EnterpriseEditEntity>>() {
            @Override
            public void onChanged(List<EnterpriseEditEntity> list) {
                initTabLayout(list);
            }
        });
    }

    /**
     * 展示顶部标题菜单
     *
     * @param list 标题列表
     */
    private void initTabLayout(List<EnterpriseEditEntity> list) {
        binding.menuRecycler.removeAllViews();
        binding.viewpager.removeAllViews();
        binding.menuRecycler.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        TabMenuAdapter adapter = new TabMenuAdapter(getActivity(), list);
        binding.menuRecycler.setAdapter(adapter);
        adapter.setListener(new TabMenuAdapter.OnTxtItemClickListener() {
            @Override
            public void onClick(int position) {
                binding.viewpager.setCurrentItem(position);
            }
        });
        ArrayList<Fragment> mFragmentList = new ArrayList<>();
        for (EnterpriseEditEntity entity : list) {
            mFragmentList.add(CorporateWebsiteListFragment.newInstance(entity.getKey()));
        }
        binding.viewpager.setAdapter(new FragPagerAdapter(getChildFragmentManager(), mFragmentList));
        binding.viewpager.setOffscreenPageLimit(list.size() - 1);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) binding.indicationLine.getLayoutParams();
        int tabWidth = ResolutionUtil.getScreenWidth(getActivity()) / 4;
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                layoutParams.leftMargin = (int) ((tabWidth - ResolutionUtil.dp2px(getActivity(), 28)) / 2 + positionOffset * tabWidth + position * tabWidth);
                binding.indicationLine.setLayoutParams(layoutParams);
            }

            @Override
            public void onPageSelected(int position) {
                adapter.setCurrentIndex(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        String key = getArguments().getString("key", "");
        if (TextUtils.isEmpty(key)) {
            binding.viewpager.setCurrentItem(0);
            adapter.setCurrentIndex(0);
        } else {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getKey().equals(key)) {
                    binding.viewpager.setCurrentItem(i);
                    adapter.setCurrentIndex(i);
                }
            }
        }
    }

    @Override
    protected void onLazyLoad() {
        viewModel.getCompanyTitleList();
    }


}